﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using SouthernLand.Common;
using SouthernLand.Common.BOL.NhaDat;

namespace SouthernLand.RaoVat
{
    public partial class RaovatByCate : Page
    {
        private int CateID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            CateID = Globals.GetIntFromQueryString("CateID");
            if (CateID <= 0)
                Response.Redirect(Globals.ApplicationPath);

            lblTitle.Text = Categories.GetCateNameByID(CateID).ToUpper();

            Pager.IndexChanged += PageIndex_Changed;
            if (!Page.IsPostBack)
            {
                DataBind_Raovat();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            rptRaovat.ItemDataBound += rptRaovat_ItemDataBound;
        }

        private void rptRaovat_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var raovat = (Raovat) e.Item.DataItem;
                if (raovat != null)
                {
                    var hplImageView = (HyperLink) e.Item.FindControl("hplImageView");
                    var imgReal = (Image) e.Item.FindControl("imgReal");
                    hplImageView.NavigateUrl = Globals.BuildUrlByRealEstateDetails(raovat.RaovatID,
                                                                                   Globals.ProcessString(
                                                                                       Globals.UnicodeToKoDau(
                                                                                           raovat.Tieude)));
                    imgReal.ImageUrl = Globals.ApplicationPath + "Raovat/Image.ashx?Size=S&ImageUrl=" + raovat.Hinh1 +
                                       "&LoaitinID=" + raovat.Loaitin;
                    imgReal.Attributes.Add("onmouseover",
                                           "showtrail('" + Globals.ApplicationPath +
                                           "Raovat/Image.ashx?Size=L&ImageUrl=" + raovat.Hinh1 + "&LoaitinID=" +
                                           raovat.Loaitin + "','" + raovat.Tieude + "',500,500)");
                    imgReal.Attributes.Add("onmouseout", "hidetrail();");
                    var lblTotalViews = (Label) e.Item.FindControl("lblTotalViews");
                    lblTotalViews.Text = raovat.TotalViews.ToString();
                    var hplRealEstateTitle = (HyperLink) e.Item.FindControl("hplRealEstateTitle");
                    hplRealEstateTitle.Text = raovat.Tieude;
                    hplRealEstateTitle.NavigateUrl = Globals.BuildUrlByRealEstateDetails(raovat.RaovatID,
                                                                                         Globals.ProcessString(
                                                                                             Globals.UnicodeToKoDau(
                                                                                                 raovat.Tieude)));
                }
            }
        }

        private void PageIndex_Changed(Object sender, EventArgs e)
        {
            DataBind_Raovat();
        }

        public void DataBind_Raovat()
        {
            Pager.PageSize = 15;
            List<Raovat> obj = Raovat.GetRaovatByCateID(CateID, Pager.PageIndex, Pager.PageSize);
            if (obj != null)
            {
                rptRaovat.DataSource = obj;
                rptRaovat.DataBind();
            }
            int TotalRecords = 0;
            foreach (Raovat f in obj)
            {
                TotalRecords = f.TotalRecords;
                break;
            }

            if (TotalRecords <= 0)
            {
                PanelMsg.Visible = true;
                lblMsg.Text = "Không có tin nào được đăng trong mục này.";
            }


            if (TotalRecords <= Pager.PageSize)
                DisplayPager.Visible = false;

            Pager.TotalRecords = CurrentPage.TotalRecords = TotalRecords;
            CurrentPage.TotalPages = Pager.CalculateTotalPages();
            CurrentPage.PageIndex = Pager.PageIndex;
        }
    }
}